home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / NetNews Sample Code ƒ / News src / nntp.c < prev    next >
Text File  |  1990-10-12  |  2KB  |  136 lines

  1. /*
  2.  * NNTP client routines.
  3.  */
  4.  
  5. #include "nntp.h"
  6.  
  7. #define    IPPORT_NNTP    119
  8.  
  9. /* tcp prototypes */
  10. OSErr    tcp_open(char *host, int port);
  11. OSErr    tcp_create(short size);
  12. OSErr    tcp_write(char *buf, int cnt);
  13. OSErr    tcp_read(char *buf, int *cnt);
  14. OSErr    tcp_close(void);
  15. OSErr    tcp_release(void); 
  16. void    tcp_shutdown(void);
  17.  
  18.  
  19. /*
  20.  * server_init  Get a connection to the remote news server.
  21.  *
  22.  *    Parameters:    "machine" is the machine to connect to.
  23.  *
  24.  *    Returns:    -1 on error
  25.  *            server's initial response code on success.
  26.  *
  27.  *    Side effects:    Connects to server.
  28.  */
  29.  
  30. server_init(machine, imsg, isize)
  31. char    *machine, *imsg;
  32. {
  33.     if (tcp_open(machine, IPPORT_NNTP) != noErr)
  34.         return -1;
  35.  
  36.     /* Now get the server's signon message */
  37.     (void) get_server(imsg, isize);
  38.     
  39.     return 0;
  40. }
  41.  
  42. /*
  43.  * put_server -- send a line of text to the server
  44.  *
  45.  *    Parameters:    "string" is the string to be sent to the
  46.  *            server.
  47.  *
  48.  *    Returns:    Nothing.
  49.  *
  50.  *    Side effects:    Talks to the server.
  51.  *
  52.  *    Note:        This routine flushes the buffer each time
  53.  *            it is called.  For large transmissions
  54.  *            (i.e., posting news) don't use it.  Instead,
  55.  *            do the fprintf's yourself, and then a final
  56.  *            fflush.
  57.  */
  58.  
  59. void
  60. put_server(string)
  61. char *string;
  62. {
  63.     tcp_write(string, strlen(string));
  64. }
  65.  
  66.  
  67. /*
  68.  * get_server -- get a line of text from the server.  Strips
  69.  * CR's and LF's.
  70.  *
  71.  *    Parameters:    "string" has the buffer space for the
  72.  *            line received.
  73.  *            "size" is the size of the buffer.
  74.  *
  75.  *    Returns:    -1 on error, 0 otherwise.
  76.  *
  77.  *    Side effects:    Talks to server, changes contents of "string".
  78.  */
  79. #define IBUFSZ    1024
  80. char ibuf[IBUFSZ], *ibufp;
  81. int ibufcnt;
  82.  
  83. get_server(string, size)
  84. char    *string;
  85. int    size;
  86. {
  87.     register char *cp;
  88.     register int c;
  89.     int cnt;
  90.  
  91.     cp = string;
  92.     while (--size) {
  93.         if (ibufcnt <= 0) {        /* need another read */
  94.             cnt = IBUFSZ;
  95.             if (tcp_read(ibuf, &cnt) != noErr)
  96.                 return 0;
  97.             ibufcnt = cnt;
  98.             ibufp = ibuf;
  99.         }
  100.         c = *ibufp++;
  101.         ibufcnt--;
  102.     
  103.         if (c == '\r')
  104.             continue;
  105.         if (c == '\n')
  106.             break;
  107.         *cp++ = c;
  108.     }
  109.     *cp = '\0';
  110.         
  111.     return (0);
  112. }
  113.  
  114.  
  115. /*
  116.  * close_server -- close the connection to the server, after sending
  117.  *        the "quit" command.
  118.  *
  119.  *    Parameters:    None.
  120.  *
  121.  *    Returns:    Nothing.
  122.  *
  123.  *    Side effects:    Closes the connection with the server.
  124.  *            You can't use "put_server" or "get_server"
  125.  *            after this routine is called.
  126.  */
  127.  
  128. void
  129. close_server()
  130. {
  131.     char    ser_line[256];
  132.  
  133.     put_server("QUIT\r\n");
  134.     (void) get_server(ser_line, sizeof(ser_line));
  135. }
  136.